package com.aprivate.leo.libannotationcompiler;

import com.aprivate.leo.libannotation.BindPath;
import com.google.auto.service.AutoService;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.tools.JavaFileObject;

@AutoService(Processor.class)
public class AnnotationCompiler extends AbstractProcessor {

    private Filer mFiler;


    @Override
    public synchronized void init(ProcessingEnvironment processingEnvironment) {
        super.init(processingEnvironment);
        mFiler = processingEnvironment.getFiler();
    }


    @Override
    public SourceVersion getSupportedSourceVersion() {
        return processingEnv.getSourceVersion();
    }


    @Override
    public Set<String> getSupportedAnnotationTypes() {
        Set<String> set = new HashSet<>();
        set.add(BindPath.class.getCanonicalName());
        return set;
    }

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(BindPath.class);
        Map<String,String> map = new HashMap<>();
        for (Element element : elements) {
            String key = ((TypeElement) element).getAnnotation(BindPath.class).value();
            String value = ((TypeElement) element).getQualifiedName().toString();
            map.put(key, value+".class");
        }
        Writer writer=null;
        if(map.size()>0){
            try {
                String activityName="ActivityUtil"+System.currentTimeMillis();
                JavaFileObject file = mFiler.createSourceFile("com.aprivate.leo.activityutils."+activityName);
                writer = file.openWriter();
                writer.write("package com.aprivate.leo.activityutils;\n" +
                        "\n" +
                        "import android.app.Activity;\n" +
                        "\n" +
                        "import com.aprivate.leo.libarouter.IRouter;\n" +
                        "import com.aprivate.leo.libarouter.Router;\n" +
                        "\n" +
                        "/**\n" +
                        " * Author: Administrator\n" +
                        " * Date: 2020/2/10\n" +
                        " * Project: ModularizationTemplateApp\n" +
                        " * Description:\n" +
                        " */\n" +
                        "public class "+activityName+" implements IRouter {\n" +
                        "    @Override\n" +
                        "    public void putActivities() {\n");

                Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, String> next = iterator.next();
                    writer.write("Router.getInstance().putActivity(\""+next.getKey()+"\","+next.getValue()+");\n");
                }


                writer.write("    }\n" +
                        "}");


            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(writer!=null){
                    try {
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ;
                }
            }
        }
        return false;
    }
}
